﻿/*=============================================================================
 TASK 4:                                                              {Methods}
 Write a method that counts how many times given number appears in given array.
 Write a test program to check if the method is working correctly.
 =============================================================================*/

using System;

class CountsGivenNumberAppears
{
    static void Main()
    {

        int n = 0;
        int[] array = { 20, 4, 1, 2, 5, 5, 6, 6, 6, 9, 10 };

        bool isFound = SearchForNumber(array, n);

        if (isFound)
        {
            int count = CountNumberAppears(array, n);
            Console.WriteLine("{0} appears {1} times in this array", n, count);
        }
        else
        {
            Console.WriteLine("Search number isn't in this array");
        }
    }

    static int GetIndex(int[] arr, int n)
    {
        Array.Sort(arr);
        int index = Array.BinarySearch(arr, n);

        return index;
    }
    static bool SearchForNumber(int[] arr, int n)
    {
        int check = GetIndex(arr,n);
        if (check >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    static int CountNumberAppears(int[] arr, int n)
    {
        int count = 0;
        int index = GetIndex(arr, n);

        foreach (var num in arr)
        {
            if (arr[index]==num)
            {
                count++;
            }
        }
        return count;
    }
}

